var dsq, app = getApp(),
    siteinfo = require("../../../../siteinfo.js");

Page({
    data: {
        selectedindex: 0,
        topnav: [{
                img: "../../../img/icon/djd.png",
                img1: "../../../img/icon/wdjd.png",
                name: "新订单",
                idx: 0
            },
            {
                img: "../../../img/icon/dps.png",
                img1: "../../../img/icon/wdps.png",
                name: "处理中",
                idx: 1
            },
            // {
            //     img: "../../../img/icon/dzt.png",
            //     img1: "../../../img/icon/wdzt.png",
            //     name: "待自提",
            //     idx: 2
            // },
            {
                img: "../../../img/icon/ywc.png",
                img1: "../../../img/icon/wywc.png",
                name: "已完成",
                idx: 3
            },
            {
                img: "../../../img/icon/sh.png",
                img1: "../../../img/icon/wsh.png",
                name: "售后/退款",
                idx: 4
            }
        ],
        open: !1,
        pagenum: 1,
        order_list: [],
        storelist: [],
        mygd: !1,
        jzgd: !0
    },
    maketel: function(t) {
        var e = t.currentTarget.dataset.tel;
        wx.makePhoneCall({
            phoneNumber: e
        });
    },
    location: function(t) {
        var e = t.currentTarget.dataset.lat,
            a = t.currentTarget.dataset.lng,
            o = t.currentTarget.dataset.address;
        wx.openLocation({
            latitude: parseFloat(e),
            longitude: parseFloat(a),
            address: o,
            name: "位置"
        });
    },
    selectednavbar: function(t) {
        this.setData({
            pagenum: 1,
            order_list: [],
            storelist: [],
            mygd: !1,
            jzgd: !0,
            selectedindex: t.currentTarget.dataset.index,
            status: Number(t.currentTarget.dataset.index) + 1
        }), this.reLoad();
    },
    doreload: function(t) {
        this.setData({
                pagenum: 1,
                order_list: [],
                storelist: [],
                mygd: !1,
                jzgd: !0,
                selectedindex: t - 1,
                status: t
            }),
            this.reLoad();
    },
    kindToggle: function(t) {
        var e = t.currentTarget.id,
            a = this.data.order_list;
        for (var o = 0, n = a.length; o < n; ++o) a[o].open = o == e && !a[o].open;
        this.setData({
            order_list: a
        });
    },
    reLoad: function() {
        var t, a = this,
            e = this.data.status || 1,
            o = wx.getStorageSync("sjdsjid"),
            n = this.data.pagenum,
            i = "";
        1 == e && (t = "2"),
            2 == e && (t = "3", i = "2"),
            3 == e && (t = "3", i = "1"),
            4 == e && (t = "4,5"),
            5 == e && (t = "6,7,8,9,10"),
            app.util.request({
                url: "entry/wxapp/StoreWmOrder",
                cachetime: "0",
                data: {
                    state: t,
                    zt: i,
                    store_id: o,
                    page: n,
                    pagesize: 10
                },
                success: function(t) {
                    t.data.length < 10 ? a.setData({
                        mygd: !0,
                        jzgd: !0
                    }) : a.setData({
                        jzgd: !0,
                        pagenum: a.data.pagenum + 1
                    });
                    var e = a.data.storelist;
                    e = function(t) {
                        for (var e = [], a = 0; a < t.length; a++) - 1 == e.indexOf(t[a]) && e.push(t[a]);
                        return e;
                    }(e = e.concat(t.data)), a.setData({
                        order_list: e,
                        storelist: e
                    });
                }
            });
    },
    onShow: function() {
        if (wx.getStorage("WMDD_Refresh") == true) {
            wx.setStorage("WMDD_Refresh", false);
            this.onLoad();
        }
    },
    onLoad: function(t) {
        var e = this,
            a = wx.getStorageSync("sjdsjid"),
            o = siteinfo.siteroot.replace("app/index.php", "");
        app.setNavigationBarColor(e), app.sjdpageOnLoad(e);
        app.util.request({
            url: "entry/wxapp/StoreLogin",
            cachetime: "0",
            data: {
                user: wx.getStorageSync("StoreName"),
                password: wx.getStorageSync("StorePassword"),
            },
            success: function(t) {
                if (null != t.data.storeid) {
                    e.reLoad();
                    e.getLoadFunc();
                } else {
                    wx.showModal({
                        title: "提示",
                        content: "系统检测到您更换绑定商家账号，请重新登录",
                        success: function(res) {
                            wx.redirectTo({
                                url: "../login"
                            })
                        }
                    });
                }
            }
        });
    },
    getLoadFunc() {
        var e = this,
            a = wx.getStorageSync("sjdsjid"),
            o = siteinfo.siteroot.replace("app/index.php", "");
        app.util.request({
            url: "entry/wxapp/system",
            cachetime: "0",
            success: function(t) {
                wx.setStorageSync("system", t.data);
                e.setData({
                    xtxx: t.data
                });
            }
        });
        dsq = setInterval(function() {
            wx.getStorageSync("yybb") ? app.util.request({
                url: "entry/wxapp/NewOrder",
                cachetime: "0",
                data: {
                    store_id: a
                },
                success: function(t) {
                    1 == t.data && wx.playBackgroundAudio({
                        dataUrl: o + "addons/zh_cjdianc/template/images/wm.wav",
                        title: "语音播报"
                    }), 2 == t.data && wx.playBackgroundAudio({
                        dataUrl: o + "addons/zh_cjdianc/template/images/dn.wav",
                        title: "语音播报"
                    });
                }
            }) : clearInterval(dsq);
        }, 1e4);
    },
    smhx: function(t) {
        var a = wx.getStorageSync("sjdsjid");
        wx.scanCode({
            success: function(t) {
                var e = "/" + t.path;
                wx.navigateTo({
                    url: e + "&storeid=" + a
                });
            },
            fail: function(t) {
                console.log("扫码fail");
            }
        });
    },
    djjd: function(t) {
        var e = this,
            a = t.currentTarget.dataset.id;
        wx.showModal({
            title: "提示",
            content: "是否确认接单？",
            cancelText: "否",
            confirmText: "是",
            success: function(t) {
                if (t.cancel) return !0;
                t.confirm && (wx.showLoading({
                    title: "操作中",
                    mask: !0
                }), app.util.request({
                    url: "entry/wxapp/JdOrder",
                    cachetime: "0",
                    data: {
                        order_id: a
                    },
                    success: function(t) {
                        "1" == t.data ? (wx.showToast({
                            title: "接单成功",
                            icon: "success",
                            duration: 1e3
                        }), setTimeout(function() {
                            e.doreload(2);
                        }, 1e3)) : wx.showToast({
                            title: "请重试",
                            icon: "loading",
                            duration: 1e3
                        });
                    }
                }));
            }
        });
    },
    jjjd: function(t) {
        var e = this,
            a = t.currentTarget.dataset.id;
        wx.showModal({
            title: "提示",
            content: "是否拒绝接单？",
            cancelText: "否",
            confirmText: "是",
            success: function(t) {
                if (t.cancel) return !0;
                t.confirm && (wx.showLoading({
                    title: "操作中",
                    mask: !0
                }), app.util.request({
                    url: "entry/wxapp/JjOrder",
                    cachetime: "0",
                    data: {
                        order_id: a
                    },
                    success: function(t) {
                        "1" == t.data ? (wx.showToast({
                            title: "操作成功",
                            icon: "success",
                            duration: 1e3
                        }), setTimeout(function() {
                            e.doreload(5);
                        }, 1e3)) : wx.showToast({
                            title: "请重试",
                            icon: "loading",
                            duration: 1e3
                        });
                    }
                }));
            }
        });
    },
    wcps: function(t) {
        var e = this,
            a = t.currentTarget.dataset.id;
        wx.showModal({
            title: "提示",
            content: "确认完成此订单？",
            cancelText: "否",
            confirmText: "是",
            success: function(t) {
                if (t.cancel) return !0;
                t.confirm && (wx.showLoading({
                    title: "操作中",
                    mask: !0
                }), app.util.request({
                    url: "entry/wxapp/OkOrder",
                    cachetime: "0",
                    data: {
                        order_id: a
                    },
                    success: function(t) {
                        "1" == t.data ? (wx.showToast({
                            title: "操作成功",
                            icon: "success",
                            duration: 1e3
                        }), setTimeout(function() {
                            e.doreload(4);
                        }, 1e3)) : wx.showToast({
                            title: "请重试",
                            icon: "loading",
                            duration: 1e3
                        });
                    }
                }));
            }
        });
    },
    jjtk: function(t) {
        var e = this,
            a = t.currentTarget.dataset.id;
        wx.showModal({
            title: "提示",
            content: "是否拒绝退款？",
            cancelText: "否",
            confirmText: "是",
            success: function(t) {
                if (t.cancel) return !0;
                t.confirm && (wx.showLoading({
                    title: "操作中",
                    mask: !0
                }), app.util.request({
                    url: "entry/wxapp/JjTk",
                    cachetime: "0",
                    data: {
                        order_id: a
                    },
                    success: function(t) {
                        "1" == t.data ? (wx.showToast({
                            title: "操作成功",
                            icon: "success",
                            duration: 1e3
                        }), setTimeout(function() {
                            e.doreload(5);
                        }, 1e3)) : wx.showToast({
                            title: "请重试",
                            icon: "loading",
                            duration: 1e3
                        });
                    }
                }));
            }
        });
    },
    tgtk: function(t) {
        var e = this,
            a = t.currentTarget.dataset.id;
        wx.showModal({
            title: "提示",
            content: "是否通过退款？",
            cancelText: "否",
            confirmText: "是",
            success: function(t) {
                if (t.cancel) return !0;
                t.confirm && (wx.showLoading({
                    title: "操作中",
                    mask: !0
                }), app.util.request({
                    url: "entry/wxapp/TkTg",
                    cachetime: "0",
                    data: {
                        order_id: a
                    },
                    success: function(t) {
                        "1" == t.data ? (wx.showToast({
                            title: "操作成功",
                            icon: "success",
                            duration: 1e3
                        }), setTimeout(function() {
                            e.doreload(5);
                        }, 1e3)) : wx.showToast({
                            title: "请重试",
                            icon: "loading",
                            duration: 1e3
                        });
                    }
                }));
            }
        });
    },
    onUnload: function() {
        clearInterval(dsq);
    },
    onPullDownRefresh: function() {},
    onReachBottom: function() {
        !this.data.mygd && this.data.jzgd && (this.setData({
            jzgd: !1
        }), this.reLoad());
    }
});